package com.software.sqlite.api.admin.v1.controller;

import com.software.sqlite.api.admin.v1.service.impl.AmV1SysUserService;
import com.software.sqlite.api.admin.v1.vo.AdminV1Result;
import com.software.sqlite.service.impl.YstMovieService;
import com.software.sqlite.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

@Api(value = "管理端-公共接口", tags = "管理端-公共接口")
@RestController
@Slf4j
@RequestMapping("/admin/v1/common")
@CrossOrigin
public class CommonController extends BaseController {

    @Resource
    private AmV1SysUserService amV1SysUserService;
    @Resource
    private YstMovieService ystMovieService;



    /**
     * 上传uploadVideo
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadImage", consumes = "multipart/form-data", headers = "content-type=multipart/form-data")
    public AdminV1Result uploadImage(MultipartFile file, HttpServletRequest request)throws Exception{
        Map<String,Object> paramsMap=this.getParameterMap(request);
//        String token= CommonUtils.parseStringData(paramsMap.get("token"));
//        AdminV1Result result=getAdmin(token);
//        if(result.getCode()==-1){ return result; }
        return AdminV1Result.success(amV1SysUserService.uploadImage(file,paramsMap,request));
    }


    /**
     * 显示不在项目目录下的图片
     *
     * @param filePath
     * @param request
     * @param response
     */
    @RequestMapping("/showImageByServer")
    public void showImageByServer(String filePath,HttpServletRequest request, HttpServletResponse response){
        amV1SysUserService.showImageByServer(filePath,request,response);
    }
    /**
     * 下载文件
     * @param filePath
     * @param request
     * @param response
     */
    @RequestMapping("/downloadApk")
    public void downloadApk(String filePath,HttpServletRequest request, HttpServletResponse response){
        amV1SysUserService.downloadApk(filePath,request,response);
    }

    /**
     * 下载文件
     *
     * @param filePath
     * @param request
     * @param response
     */
    @RequestMapping("/downloadExcel")
    public void downloadExcel(String filePath,HttpServletRequest request, HttpServletResponse response){
        amV1SysUserService.downloadExcel(filePath,request,response);
    }




    @GetMapping("/showVideo")
    public void showVideo(HttpServletRequest request, HttpServletResponse response, String videoFileName)throws Exception{
//        if(CommonUtils.isNotBlank(videoFileName)){
//            String path = request.getContextPath();
//            String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
//            response.setCharacterEncoding("utf-8");
//            response.setContentType("text/html");
//            PrintWriter out = response.getWriter();
//            out.write("<style>body{background-color: rgb(0, 0, 0);}</style><video controls autoplay muted name=\"media\"style=\"    position: absolute;\n" +
//                    "    top: 0px;\n" +
//                    "    right: 0px;\n" +
//                    "    bottom: 0px;\n" +
//                    "    left: 0px;\n" +
//                    "    max-height: 100%;\n" +
//                    "    max-width: 100%;\n" +
//                    "    margin: auto;\" ><source src=\""+basePath+"/admin/v1/common/downloadVideo?videoFileName="+videoFileName+"\" type=\"video/mp4\"></video>");
//            out.close();
//        }

    }

    @ApiOperation(value = "下载", notes = "下载")
    @GetMapping("/downloadVideo")
    public void downloadVideo(String videoFileName,HttpServletResponse response)throws Exception{
//        amV1SysUserService.downloadStreamFile(videoFileName,response);
    }

    @ApiOperation(value = "apiTest", notes = "apiTest")
    @GetMapping("/apiTest")
    public AdminV1Result curlTest(String type,String requestData)throws Exception{
        return AdminV1Result.success(ystMovieService.apiTest(type,requestData));
    }


}
